<%@page import="wt.org.WTGroup"%>
<%@page import="wt.org.OrganizationServicesHelper"%>
<%@page import="wt.org.WTPrincipal"%>
<%@page import="wt.session.SessionHelper"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="ext.acae2.dms.resource.DataExchangeResource" %>
<%@ taglib uri="http://www.ptc.com/windchill/taglib/fmt" prefix="fmt"%>

<fmt:setBundle basename="ext.acae2.dms.resource.DataExchangeResource" />
<fmt:message var="labelSupplier" key="<%= DataExchangeResource.LABEL_SUPPLIER%>"/>
<fmt:message var="labelDataPackage" key="<%= DataExchangeResource.LABEL_DATA_PACKAGE%>"/>
<fmt:message var="buttonCheck" key="<%= DataExchangeResource.LABEL_CHECK_DATA%>"/>
<fmt:message var="buttonSubmit" key="<%= DataExchangeResource.LABEL_SUBMIT%>"/>
<fmt:message var="labelUploadWarning" key="<%= DataExchangeResource.LABEL_UPLOAD_WARNING%>"/>
<fmt:message var="folderName410" key="<%= DataExchangeResource.FOLDER_NAME_410 %>"/>
<fmt:message var="folderName420" key="<%= DataExchangeResource.FOLDER_NAME_420 %>"/>
<fmt:message var="folderName430" key="<%= DataExchangeResource.FOLDER_NAME_430 %>"/>
<fmt:message var="folderName606" key="<%= DataExchangeResource.FOLDER_NAME_606 %>"/>
<fmt:message var="folderName614" key="<%= DataExchangeResource.FOLDER_NAME_614 %>"/>
<fmt:message var="folderName624" key="<%= DataExchangeResource.FOLDER_NAME_624 %>"/>
<fmt:message var="errorMessageType" key="<%= DataExchangeResource.ERROR_MESSAGE_PACKAGE_TYPE %>"/>
<fmt:message var="labelLog" key="<%= DataExchangeResource.LABEL_LOG%>"/>
<fmt:message var="transactionSubmit" key="<%= DataExchangeResource.COMFIG_TRANSACTION_SUBMIT%>"/>

<style type="text/css">
table {
    position: absolute;
    left: 100px;
    top: 20px;
}

th {
    text-align:right;
    padding: 6px;
}

td {
    padding: 6px;
}
</style>

<%
String procOid = request.getParameter("procOid");
WTPrincipal currentUser = SessionHelper.manager.getPrincipal();
WTGroup adminGroup = OrganizationServicesHelper.manager.getGroup("Administrators");
boolean isAdmin = adminGroup.isMember(currentUser);
%>

<form id="dataImport" method="post" action="/Windchill/netmarkets/jsp/ext/acae2/dms/processUpload.jsp" 
    enctype="multipart/form-data" target="hidden_frame">
<table>
    <tr>
        <th>${labelSupplier}: </th>
        <td>
            <select id="supplier" name="supplier">
                <option value="606">${folderName606}</option>
                <option value="614">${folderName614}</option>
                <option value="624">${folderName624}</option>
<%--                 <option value="410">${folderName410}</option> --%>
<%--                 <option value="420">${folderName420}</option> --%>
<%--                 <option value="430">${folderName430}</option> --%>
            </select>
        </td>
        <td></td>
    </tr>
    <tr>
        <th>${labelDataPackage}: </th>
        <td>
            <input id="uploadFile" name="uploadFile" type="file" onchange="checkFile()"/>
        </td>
        <td>${labelUploadWarning}</td>
    </tr>
    <%if(isAdmin){%>
    <tr>
        <th>${transactionSubmit}:</th>
        <td>
            <input id="transactionSubmitFlag" name="transactionSubmitFlag" type="checkbox" onclick="dateChange(this)"/>
        </td>
    </tr>
    <%} %>
    <tr>
        <th></th>
        <td>
            <input id="checkButton" type="button" value="${buttonCheck}" disabled="disabled" onclick="checkXml()">
            <input id="submitButton" type="button" value="${buttonSubmit}" disabled="disabled" onclick="submitXml()">
        </td>
        <td></td>
    </tr>
    <tr>
        <th>${labelLog}: </th>
        <td>
            <textarea id="log" cols="60" rows="8" readonly="readonly" style="overflow: scroll"></textarea>
        </td>
        <td></td>
    </tr>
</table>
</form>

<iframe name="hidden_frame" id="hidden_frame" style="display:none">
</iframe>

<script>
function checkXml() {
	document.getElementById("log").value = "loading......";
	var supplier = document.getElementById("supplier");
     var currentForm = document.getElementById("dataImport");
     currentForm.action = "/Windchill/netmarkets/jsp/ext/acae2/dms/processCheck.jsp?supplier="+supplier;
     currentForm.method = "post";
     currentForm.submit();
}

	function submitXml() {
		document.getElementById("log").value = "loading......";
		var currentForm = document.getElementById("dataImport");
		currentForm.action = "/Windchill/netmarkets/jsp/ext/acae2/dms/processUpload.jsp?procOid=<%=procOid%>";
		currentForm.method = "post";
		currentForm.submit();
	}

	function checkFile() {
		var filePath = document.getElementById("uploadFile").value;
		var fileType = filePath.substring(filePath.lastIndexOf('.') + 1);

		if (fileType.toUpperCase() != "ZIP" && fileType.toUpperCase() != "RAR") {
			alert("${errorMessageType}");
			return;
		} else {
			document.getElementById("checkButton").disabled = false;
		}
	}

	function callback(msg) {
		document.getElementById("log").value = msg;
	}

	function changeButton() {
		document.getElementById("submitButton").disabled = false;
	}

	function dateChange(obj) {
		if (!obj.value) {
			obj.value = 'Y';
			obj.checked = 'checked';
		} else {
			obj.value = '';
			obj.checked = '';
		}
	}
	
</script>
